package com.fastbee.oss.service.impl;

import java.util.List;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.recorder.FileRecorder;
import com.fastbee.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.oss.mapper.OssDetailMapper;
import com.fastbee.oss.domain.OssDetail;
import com.fastbee.oss.service.IOssDetailService;

/**
 * 文件记录Service业务层处理
 *
 * @author zhuangpeng.li
 * @date 2023-03-30
 */
@Service
public class OssDetailServiceImpl implements FileRecorder, IOssDetailService
{
    @Autowired
    private OssDetailMapper ossDetailMapper;

    /**
     * 查询文件记录
     *
     * @param id 文件记录主键
     * @return 文件记录
     */
    @Override
    public OssDetail selectOssDetailById(Integer id)
    {
        return ossDetailMapper.selectOssDetailById(id);
    }

    /**
     * 查询文件记录列表
     *
     * @param ossDetail 文件记录
     * @return 文件记录
     */
    @Override
    public List<OssDetail> selectOssDetailList(OssDetail ossDetail)
    {
        return ossDetailMapper.selectOssDetailList(ossDetail);
    }

    /**
     * 新增文件记录
     *
     * @param ossDetail 文件记录
     * @return 结果
     */
    @Override
    public int insertOssDetail(OssDetail ossDetail)
    {
        ossDetail.setCreateTime(DateUtils.getNowDate());
        return ossDetailMapper.insertOssDetail(ossDetail);
    }

    /**
     * 修改文件记录
     *
     * @param ossDetail 文件记录
     * @return 结果
     */
    @Override
    public int updateOssDetail(OssDetail ossDetail)
    {
        ossDetail.setUpdateTime(DateUtils.getNowDate());
        return ossDetailMapper.updateOssDetail(ossDetail);
    }

    /**
     * 批量删除文件记录
     *
     * @param ids 需要删除的文件记录主键
     * @return 结果
     */
    @Override
    public int deleteOssDetailByIds(Integer[] ids)
    {
        return ossDetailMapper.deleteOssDetailByIds(ids);
    }

    /**
     * 删除文件记录信息
     *
     * @param id 文件记录主键
     * @return 结果
     */
    @Override
    public int deleteOssDetailById(Integer id)
    {
        return ossDetailMapper.deleteOssDetailById(id);
    }

    @Override
    public boolean record(FileInfo fileInfo) {
        OssDetail ossDetail = new OssDetail(fileInfo);
        ossDetail.setCreateTime(DateUtils.getNowDate());
        return ossDetailMapper.insertOssDetail(ossDetail) == 1;
    }

    @Override
    public FileInfo getByUrl(String url) {
        return ossDetailMapper.selectOssDetailByUrl(url).tofileInfo();
    }

    @Override
    public boolean delete(String url) {
        OssDetail ossDetail = ossDetailMapper.selectOssDetailByUrl(url);
        if (ossDetail != null) {
            return ossDetailMapper.deleteOssDetailById(ossDetail.getId()) == 1;
        }
        return false;
    }
}
